<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户列表</title>
<%@ include file="/WEB-INF/jsp/common/inc.jsp" %>
<link rel="stylesheet" href="${_resources}bootstrap/bootstrap.min.css" />
<link rel="stylesheet" href="${_resources}bootstrap/bootstrap-table.min.css" />
<script type="text/javascript" src="${_resources}jquery-ui/jquery-ui-1.10.4.custom.min.js"></script>
<link rel="stylesheet" href="${_resources}jquery-ui/jquery-ui-1.10.4.custom.min.css" />
<script type="text/javascript" src="${_resources}JSON-js-master/json2.js"></script>
<script type="text/javascript">
var menuList;
var deptNameMap;
var numCode = "${numCode}";
var userId="<%=session.getAttribute("user_id")%>";
var userName="<%=session.getAttribute("userName")%>";

if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length >>> 0;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}


$(function (){
	menuList = $("#menuList").bootstrapTable({
		url : '${_baseUrl}/userController/select?random='+Math.random(),
		pagination: true,
		pageList: [5, 10, 20, 50],
  		queryParams: queryParams,
        columns: [{
        	 field: 'checked',
             checkbox: true
         }, {
            field: 'id',
            title: 'id',
            visible:false,
            width: 5
        }, {
            field: 'userName',
            title: '姓名',
            width: 90,
            formatter : function(value, row, index) {
            	var str="";
				str +="<a onClick=\"editFun('"+row.id+"');\">"+value+"</a>";  
				return str;
			}
        },
        {
            field: 'userId',
            title: '用户账号',
            width: 90
        },
        {
            field: 'deptName',
            title: '部门',
            width: 80/* ,
            formatter : function(value, row, index) {
            	var str='';
            	for(var i=0; i<deptNameMap.length; i++){
					if (deptNameMap[i].id == value) return deptNameMap[i].deptName;
					else return '-';      
				}
				return str;
			} */
        },{

            field: 'post',
            title: '职务',
            width: 80
        },{

            field: 'email',
            title: '电子邮箱',
            width: 100
        },{

            field: 'officeTelephone',
            title: '办公电话',
            width: 100
        },
        {
            field: 'userStatus',
            title: '用户状态',
            width: 65,
            formatter : function(value, row, index) {
            	if(row.userLock =='1'){
            		return "锁定";
            	}
            	if("1" == value){
            		return "禁用";
            	}else if("0" == value){
            		return "启用";
            	}
				/* if (1== value) {
					return "<a onClick=\"statusFun('"+row.id+"','"+value+"');\">禁用</a>";
				}
				return "<a onClick=\"statusFun('"+row.id+"','"+value+"');\">启用</a>"; */
			}
        },
        {
            field: 'sort',
            title: '上移',
            width: 60,
            formatter : function(value, row, index) {
				return "<a onClick=\"sortFun('"+row.id+"','"+value+"','0');\">上移</a>";
			}
        },
        {
            field: 'sort',
            title: '下移',
            width: 60,
            formatter : function(value, row, index) {
					return "<a onClick=\"sortFun('"+row.id+"','"+value+"','1');\">下移</a>";
			}
        }]
    });
	
	function queryParams(params){
		return {
		    pageSize: params.limit,
		    pageNo: params.pn,
		    sort : params.sort,
		    order : params.order,
		    userNameForSerch:$("#userNameForSerch").val(),
            departId : $("#depart option:selected").val() == '' ? null : $("#depart option:selected").val(),
            post : $("#post option:selected").val() == '' ? null : $("#post option:selected").val(),
        }
	}
	
	$("#userNameForSerch").keydown(function(event) {//给输入框绑定按键事件
	    if(event.keyCode == "13") {//判断如果按下的是回车键则执行下面的代码
	    	refreshTable();
	  	}
	})
	
});
function addFun(){
	/* $("#popIframe").empty();	
	$("#popDiv").dialog({
		title:'添加用户',
		autoOpen: true,
		modal: true,	
		height: 500,
		width: 900
	});	
	$("#popIframe").attr("width","100%");
	$("#popIframe").attr("height","95%");
	$("#popIframe").attr("src","${_baseUrl}/userController/addPage");
	$("#popIframe").load(function(){  // 弹框大小自适应内容宽高 
		var contH = $(this).contents().find("body").height()+42;
		$(this).parent().height(contH);
	}) */
	layerIndex =layer.open({
        type: 2 //Page层类型
        ,area: ['900px', '480px']
        ,title: '添加用户'
        ,shade: 0.3 //遮罩透明度
        ,maxmin: true //允许全屏最小化
        ,anim: -1 //0-6的动画形式，-1不开启
        ,content: "${_baseUrl}/userController/addPage?random="+Math.random()
    });
}
function editFun(id){
	/* $("#popIframe").empty();	
	$("#popDiv").dialog({
		title:'修改用户',
		autoOpen: true,
		modal: true,	
		height: 500,
		width: 900
	});	
	$("#popIframe").attr("width","100%");
	$("#popIframe").attr("height","95%");
	$("#popIframe").attr("src","${_baseUrl}/userController/editPage?id="+id);
	$("#popIframe").load(function(){  // 弹框大小自适应内容宽高 
		var contH = $(this).contents().find("body").height()+42;
		$(this).parent().height(contH);
	}) */
	layerIndex =layer.open({
        type: 2 //Page层类型
        ,area: ['900px', '460px']
        ,title: '修改用户'
        ,shade: 0.3 //遮罩透明度
        ,maxmin: true //允许全屏最小化
        ,anim: -1 //0-6的动画形式，-1不开启
        ,content: "${_baseUrl}/userController/editPage?id="+id+"&random="+Math.random()
    });
}
function editPwdFun(userId){
	/* $("#popIframe").empty();	
	$("#popDiv").dialog({
		title:'修改密码',
		autoOpen: true,
		modal: true,	
		height: 500,
		width: 800
	});	
	$("#popIframe").attr("width","100%");
	$("#popIframe").attr("height","95%");
	$("#popIframe").attr("src","${_baseUrl}/userController/editPwdPage?userId="+userId);    
	$("#popIframe").load(function(){  // 弹框大小自适应内容宽高 
		var contH = $(this).contents().find("body").height()+42;
		$(this).parent().height(contH);
	}) */
	layerIndex =layer.open({
        type: 2 //Page层类型
        ,area: ['900px', '440px']
        ,title: '修改密码'
        ,shade: 0.3 //遮罩透明度
        ,maxmin: true //允许全屏最小化
        ,anim: -1 //0-6的动画形式，-1不开启
        ,content: "${_baseUrl}/userController/editPwdPage?userId="+userId+"&random="+Math.random()
    });
}
/* 重置密码 */
var userIdName = "${userId}";
function pwdResetFun(){
	var userId = "" ;
	var a = 0;
	var rows= $("#menuList").bootstrapTable('getSelections');
	if(rows.length==0){
		layer.msg("请先选择人员");
	}else{
		for(var i = 0; i < rows.length; i++){
			userId+= rows[i].id+",";
			if(userIdName == rows[i].userId && userIdName == "admin"){
				a = 1;
			}
		}
		
		if(a == 1){
			layer.msg("系统管理员不能重置自己的密码");
			return;
		}
		var rowsString = JSON.stringify(rows);
		/*if(numCode == 'tianjin712suo' && userName != '超级管理员'){
			layer.msg("重置成功,待审批");
			sendMessage(userId);
			return;
		}*/
        if(numCode == 'tianjin712suo' /*&&(userName == '超级管理员'|| userName == '系统管理员')*/){ //修改bug [#15852] 有重置密码权限的用户重置密码后都应发代办消息
            layer.msg("重置成功,待审批");
            sendMessage(userId);
            return;
        }
		var path="${_baseUrl}/userController/PwdReset";
		 $.ajax({    
				type: "POST",    
				async: false,    
				url:path,   
				data:{
					"userId":userId
				},    
				dataType:"text",    
				success: function (data) {
					layer.msg(data);
					refreshTable();//刷新当前页面.		
				}   
			});  
	}  
}
function sendMessage(userId){
	 var path="${_baseUrl}/userController/sendMessage";
	 $.ajax({    
		type: "POST",    
		async: false,    
		url:path,   
		data:{
			"userId":userId ,
		},    
		success: function () {   
		} ,
		error:function(json){
			layer.msg("系统错误,请联系管理员");
		}  
	}); 
}

function statusFun(status){
	var selected = menuList.bootstrapTable('getSelections');
	if(selected.length < 1){
		layer.msg("请选择要操作的数据");
		return false;
	}
	if(status=='1'){
		var rowsString = JSON.stringify(selected); 
		if(rowsString.indexOf("系统管理员")>0||rowsString.indexOf("安全保密员")>0||rowsString.indexOf("安全审计员")>0){
			layer.msg("平台初始化用户，不可操作");
			return false;
		}
	}
	
	var ids = [];
	for(i in selected){
		if(userId == selected[i].id && status=='1'){
			layer.msg("无法禁用当前用户");
			return false;
		}
		ids.push(selected[i].id);
	}
	
	var jy= false;
	var qy = false;
	for(i in selected){
		if(selected[i].userStatus == 1) {
			jy = true;
		}
		if(selected[i].userStatus == 0) {
			qy = true;
		}
	}
	if(status=='1' && jy == true){
		layer.msg("选择的用户中已存在禁用状态的用户");
		return false;
	}else if(status=='0' && qy == true) {
		layer.msg("选择的用户中已存在启用状态的用户");
		return false;
	} else{
		var path="${_baseUrl}/userController/updateStatus";
		 $.ajax({    
				type: "POST",    
				async: false,    
				url:path,   
				data:{
					"ids":ids.join(","),
					"userStatus":status
				},    
				dataType:"json",    
				success: function (data) {
					layer.msg(data.message,{shift:5,time:800},function(){
						refreshTable();//刷新当前页面.		
					});
				}   
		});  
	}
}
/* 排序 */
function sortFun(id,sort,type){
	 var path="${_baseUrl}/userController/updateSort";
	 $.ajax({    
			type: "POST",    
			async: false,    
			url:path,   
			data:{
				"id":id,
				"sort":sort,
				"type":type
			},    
			dataType:"text",    
			success: function (data) {   
				layer.msg(data,{shift:5,time:1000},function(){
					refreshTable();//刷新当前页面.		
				});
			}   
		});                
}
function unlockFun(){
	var rows= $("#menuList").bootstrapTable('getSelections');
	if(rows.length==0){
		layer.msg("请先选择解锁人员");
	}else{
		var rowsString = JSON.stringify(rows);
		var path="${_baseUrl}/userController/updateLock";
		 $.ajax({    
				type: "POST",    
				async: false,    
				url:path,   
				data:{
					"rows":rowsString
				},    
				dataType:"text",    
				success: function (data) {   
					layer.msg("解锁成功");
					refreshTable();//刷新当前页面.		
				}   
			});  
	}            
}
/* 注销用户  */
function logoutFun(){
	var rows= $("#menuList").bootstrapTable('getSelections');
	//系统管理员、安全保密员、安全审计员
	
	if(rows.length==0){
		layer.msg("请先选择注销人员");
	}else{
		var logOut="1";
		var rowsString = JSON.stringify(rows); 
		if(rowsString.indexOf("系统管理员")>0||rowsString.indexOf("安全保密员")>0||rowsString.indexOf("安全审计员")>0){
			layer.msg("平台初始化用户，不可操作");
			return false;
		}
		for(i in rows){
			if(userId == rows[i].id){
				layer.msg("无法禁用当前用户");
				return false;
			}
		}
		var path="${_baseUrl}/userController/updateLogout";
		 $.ajax({    
				type: "POST",    
				async: false,    
				url:path,   
				data:{
					"rows":rowsString,
					"logOut":logOut
				},    
				dataType:"text",    
				success: function (data) {
					if(data == "0"){
						layer.msg("注销成功");
					}
					refreshTable();//刷新当前页面.		
				}   
			}); 
	}
}

/**
 * 批量导入用户
 */
function batchImport(){
	/* $("#popIframe").empty();	
	$("#popDiv").dialog({
		title:'批量导入',
		autoOpen: true,
		modal: true,	
		height: 400,
		width: 700       
	});	
	$("#popIframe").attr("width","100%");
	$("#popIframe").attr("height","95%");
	$("#popIframe").attr("src","${_baseUrl}/userController/showBatchImport?rand="+Math.random()); 
	
	$("#popIframe").load(function(){  // 弹框大小自适应内容宽高 
		var contH = $(this).contents().find("body").height()+40;
		$(this).parent().height(contH);
	}) */
	layer.open({
        type: 2 //Page层类型
        ,area: ['600px', '200px']
        ,title: '批量导入用户'
        ,shade: 0.3 //遮罩透明度
        ,maxmin: true //允许全屏最小化
        ,anim: -1 //0-6的动画形式，-1不开启
        ,content: "${_baseUrl}/userController/showBatchImport?rand="+Math.random()
    });
}

/* 关闭弹出框 */
function closeWin(){
	//$("#popDiv").dialog('close');
	layer.closeAll();
}

function showme(){
    var div = document.getElementById("switch");
    if (div.style.display == "block") {
        $(".advanced_search_btn").removeClass("stop");
        div.style.display = "none";
    } else if (div.style.display == "none") {
        div.style.display = "block";
        $(".advanced_search_btn").addClass("stop");
    }
}


function refreshTable(){
	menuList.bootstrapTable('refresh');
}
function excelInFun(){
	layer.open({
        type: 2 //Page层类型
        ,area: ['600px', '200px']
        ,title: '导入用户身份证号码'
        ,shade: 0.3 //遮罩透明度
        ,maxmin: true //允许全屏最小化
        ,anim: -1 //0-6的动画形式，-1不开启
        ,content: "${_baseUrl}/userController/showExcelImportIdcast?rand="+Math.random()
    });
}
</script>
<style type="text/css">
.func_search_div{width: 250px;}
.func_search input{width: 215px;}
.fixed-table-container{border: 0px solid #ddd;}
.func_select{margin-left: 0%;}
</style>
</head>
<body>
<div class="submenu">
    <div  class="func_header">
        <span title="用户管理>用户列表">当前位置： <a href="javascript:;">用户管理</a>>用户列表</span>
    </div>
    <div class="func_bar">
        <div class="search_container">
		    <div class="func_search">
		    	<div class="func_search_div"></div>
	            <input type="text" id="userNameForSerch" name="user_date" value="" placeholder="请输入用户名称" />
			   	<a class="search_btn" id="search" href="javascript:;" onclick="refreshTable();"></a>
	        </div>
            <select style="margin-left: 10px;" id="depart" onchange="refreshTable()" class="func_select">
					<option value=''>部门</option>
					<c:forEach var="depart" items="${departmentList }">
						<option value='${depart.id}'>${depart.deptName}</option>
					</c:forEach>
			</select>
			<select id="post" onchange="refreshTable()" class="func_select">
					<option value=''>职务</option>
					<c:forEach var="post" items="${postList}">
						<option value='${post}'>${post}</option>
					</c:forEach>
			</select>
        </div>
        <div class="btn_container">
        	<shiro:hasPermission name="userController:addPage">
		        <a href="javascript:;" class="blue_btn" onClick="addFun();">添加人员</a>
		    </shiro:hasPermission>
		    <shiro:hasPermission name="userController:updateLogout">
	            <a class="blue_btn" id="cancellationPersonnel" onclick="logoutFun()" href="javascript:;">注销人员</a>
		    </shiro:hasPermission>
		    <shiro:hasPermission name="userController:PwdReset">
	            <a class="blue_btn" id="passwordReset" href="javascript:;" onclick="pwdResetFun()">密码重置</a>
		    </shiro:hasPermission>
		     <shiro:hasPermission name="userController:showBatchImport">
	            <a class="blue_btn" id="batchImport" href="javascript:;" onClick="batchImport();">批量导入</a>
		     </shiro:hasPermission>
		    <shiro:hasPermission name="userController:updateLock">
	            <a class="blue_btn" id="unlock" href="javascript:;" onclick="unlockFun()">解锁</a>
		    </shiro:hasPermission>
		    <shiro:hasPermission name="userController:updateStatus_0">
	            <a class="blue_btn" id="enable" href="javascript:;" onClick="statusFun('0');">启用</a>
		    </shiro:hasPermission>
		    <shiro:hasPermission name="userController:updateStatus_1">
	            <a class="blue_btn" id="disable" href="javascript:;" onclick="statusFun('1');" >禁用</a>
		    </shiro:hasPermission>
			<c:if test="${customCode eq 'tianjin712suo' }">
				<shiro:hasPermission name="userController:showExcelImportIdcast">
					 <a class="blue_btn" id="excelIn" href="javascript:;" onclick="excelInFun();" >导入身份证号码</a>
				</shiro:hasPermission>
			</c:if>
        </div>
    </div>
    <div style="overflow: auto; width:98%;margin: auto;">
	    <table id="menuList" class="table table-bordered table_inline"></table>
	</div>
</div>
</body>
</html>